﻿<%@ WebHandler Language="C#" Class="HandlerConfig" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using BLL;
using DataContext;
using Utils;

public class HandlerConfig : IHttpHandler, IReadOnlySessionState
{

    public void ProcessRequest(HttpContext context)
    {
        string type = context.Request.QueryString["t"] ?? string.Empty;

        switch (type)
        {
            //Admin
            case "GetAdminInfoPersonal":
                GetAdminInfoPersonal(context);
                break;
            case "GetAdminInfo":
                GetAdminInfo(context);
                break;
            case "GetListAdmin":
                GetListAdmin(context);
                break;
            case "GetListAdminAll":
                GetListAdminAll(context);
                break;

            //Role
            case "GetListRole":
                GetListRole(context);
                break;
            case "GetRoleInfo":
                GetRoleInfo(context);
                break;

            //permission
            case "GetListRoleInAdmin":
                GetListRoleInAdmin(context);
                break;
            case "GetListRoleNotInAdmin":
                GetListRoleNotInAdmin(context);
                break;
            case "GetListAdminInRole":
                GetListAdminInRole(context);
                break;
            case "GetListAdminNotInRole":
                GetListAdminNotInRole(context);
                break;

            //Function
            case "GetListFunction":
                GetListFunction(context);
                break;
            case "GetListFunctionForLog":
                GetListFunctionForLog(context);
                break;
            case "GetListFunctionParrent":
                GetListFunctionParrent(context);
                break;
            case "GetFunctionInfo":
                GetFunctionInfo(context);
                break;

            //rolefunction
            case "GetListRoleInMenu":
                GetListRoleInMenu(context);
                break;
            case "GetListRoleNotInMenu":
                GetListRoleNotInMenu(context);
                break;
            case "GetListMenuNotInRole":
                GetListMenuNotInRole(context);
                break;
            case "GetListMenuInRole":
                GetListMenuInRole(context);
                break;
                //log
            case "GetListLog":
                GetListLog(context);
                break;
        }
    }

    #region admin
    public void GetAdminInfoPersonal(HttpContext context)
    {
        CtrAdmin ctrAdmin = new CtrAdmin();
        var infoAdmin = ctrAdmin.GetInfoAdminByAdminId(AdminUtils.AdminID);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(infoAdmin);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void GetAdminInfo(HttpContext context)
    {
        CtrAdmin ctrAdmin = new CtrAdmin();
        int adminID = Convert.ToInt32(context.Request.QueryString["adminID"] ?? "-1");
        var infoAdmin = ctrAdmin.GetInfoAdminByAdminId(adminID);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(infoAdmin);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void GetListAdmin(HttpContext context)
    {
        CtrAdmin ctrAdmin = new CtrAdmin();
        int currentPage = Convert.ToInt32(context.Request.QueryString["currentPage"] ?? "1");
        int pageSize = Convert.ToInt32(context.Request.QueryString["pageSize"] ?? "10");
        string userName = context.Request.QueryString["userName"] ?? "";
        int adminID = Convert.ToInt32(context.Request.QueryString["adminID"] ?? "-1");
        int roleID = Convert.ToInt32(context.Request.QueryString["roleID"] ?? "-1");
        short status = Convert.ToInt16(context.Request.QueryString["status"] ?? "-1");
        string fromdate = context.Request.QueryString["fromDate"] ?? "";
        string todate = context.Request.QueryString["toDate"] ?? "";
        DateTime fromDate = (DateTime)Library.Lib.GetDateTime(fromdate);
        DateTime toDate = (DateTime)Library.Lib.GetDateTime(todate);
        var listAdmin = ctrAdmin.GetListAdmin(userName, adminID, roleID, status, fromDate, toDate, currentPage, pageSize);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listAdmin);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void GetListAdminAll(HttpContext context)
    {
        CtrAdmin ctrAdmin = new CtrAdmin();
        var listAdmin = ctrAdmin.GetListAdminAll();
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listAdmin);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    #endregion

    #region role
    public void GetListRole(HttpContext context)
    {
        CtrRole ctrRole = new CtrRole();
        var listRole = ctrRole.GetListRole();
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listRole);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void GetRoleInfo(HttpContext context)
    {
        CtrRole ctrRole = new CtrRole();
        int roleID = Convert.ToInt32(context.Request.QueryString["roleID"] ?? "0");
        var listRole = ctrRole.GetRoleInfo(roleID);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listRole);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }
    #endregion

    #region permission
    public void GetListRoleInAdmin(HttpContext context)
    {
        CtrPermission ctrPermission = new CtrPermission();
        int adminID = Convert.ToInt32(context.Request.QueryString["adminID"] ?? "-1");
        var listRole = ctrPermission.GetListRoleInAdmin(adminID);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listRole);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void GetListRoleNotInAdmin(HttpContext context)
    {
        CtrPermission ctrPermission = new CtrPermission();
        int adminID = Convert.ToInt32(context.Request.QueryString["adminID"] ?? "-1");
        var listRole = ctrPermission.GetListRoleNotInAdmin(adminID);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listRole);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void GetListAdminInRole(HttpContext context)
    {
        CtrPermission ctrPermission = new CtrPermission();
        int roleID = Convert.ToInt32(context.Request.QueryString["roleID"] ?? "0");
        var listRole = ctrPermission.GetListAdminInRole(roleID);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listRole);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void GetListAdminNotInRole(HttpContext context)
    {
        CtrPermission ctrPermission = new CtrPermission();
        int roleID = Convert.ToInt32(context.Request.QueryString["roleID"] ?? "0");
        var listRole = ctrPermission.GetListAdminNotInRole(roleID);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listRole);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    #endregion

    #region Function
    public void GetListFunction(HttpContext context)
    {
        CtrFunction ctrFunction = new CtrFunction();
        var listFunction = ctrFunction.GetListFunction();
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listFunction);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void GetListFunctionForLog(HttpContext context)
    {
        CtrFunction ctrFunction = new CtrFunction();
        var listFunction = ctrFunction.GetListFunctionForLog();
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listFunction);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void GetListFunctionParrent(HttpContext context)
    {
        CtrFunction ctrFunction = new CtrFunction();
        var listFunction = ctrFunction.GetChild(-1, -1);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listFunction);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void GetFunctionInfo(HttpContext context)
    {
        CtrFunction ctrFunction = new CtrFunction();
        int FunctionID = Convert.ToInt32(context.Request.QueryString["functionID"] ?? "0");
        var infoFunction = ctrFunction.GetInfoFunction(FunctionID);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(infoFunction);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }
    #endregion

    #region rolefunction
    public void GetListRoleInMenu(HttpContext context)
    {
        CtrRoleFunction ctrRolefunction = new CtrRoleFunction();
        int menuID = Convert.ToInt32(context.Request.QueryString["menuID"] ?? "0");
        var listRole = ctrRolefunction.GetListRoleInFunction(menuID);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listRole);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void GetListRoleNotInMenu(HttpContext context)
    {
        CtrRoleFunction ctrRolefunction = new CtrRoleFunction();
        int menuID = Convert.ToInt32(context.Request.QueryString["menuID"] ?? "0");
        var listRole = ctrRolefunction.GetListRoleNotInFunction(menuID);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listRole);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void GetListMenuNotInRole(HttpContext context)
    {
        CtrRoleFunction ctrRole = new CtrRoleFunction();
        int roleID = Convert.ToInt32(context.Request.QueryString["roleID"] ?? "0");
        var listRole = ctrRole.GetFunctionNotInRole(roleID);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listRole);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    public void GetListMenuInRole(HttpContext context)
    {
        CtrRoleFunction ctrRole = new CtrRoleFunction();
        int roleID = Convert.ToInt32(context.Request.QueryString["roleID"] ?? "0");
        var listRole = ctrRole.GetFunctionInRole(roleID);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(listRole);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }

    #endregion
    /// <summary>
    /// Lấy danh sách nhật ký sử dụng
    /// </summary>
    /// <param name="context"></param>
    public void GetListLog(HttpContext context)
    {
        CtrLog ctrLog = new CtrLog();
        string keyWord = context.Request.QueryString["keyWord"] ?? string.Empty;
        int adminID = Convert.ToInt32(context.Request.QueryString["adminID"] ?? "-1");
        int functionID = Convert.ToInt32(context.Request.QueryString["functionID"] ?? "-1");
        char action = Convert.ToChar(context.Request.QueryString["action"] ?? "O");
        int servicesID = Convert.ToInt32(context.Request.QueryString["servicesID"] ?? "-1");
        DateTime? fromDate = Library.Lib.GetDateTime(context.Request.QueryString["fromDate"] ?? string.Empty);
        DateTime? toDate = Library.Lib.GetDateTime(context.Request.QueryString["toDate"] ?? string.Empty);
        int currentPage = Convert.ToInt32(context.Request.QueryString["currentPage"] ?? "1");
        int pageSize = Convert.ToInt32(context.Request.QueryString["pageSize"] ?? "10");
        if (!fromDate.HasValue || !toDate.HasValue) return;
        var objReturn = ctrLog.GetListLog(keyWord, adminID, functionID, action, servicesID, fromDate.Value, toDate.Value, currentPage, pageSize);
        string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(objReturn);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonString);
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
